﻿

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using My.Core.Model.Models;
using My.Core.Utils;
using System;
using System.Collections.Concurrent;
using System.Diagnostics;



namespace My.Core.Utils
{
    public class EFLoggerFactory : ILoggerFactory
    {
        internal ConcurrentBag<ILoggerProvider> LoggerProviders { get; private set; }
        public void AddProvider(ILoggerProvider provider)
        {

        }

        public ILogger CreateLogger(string categoryName)
        {
            return new EFLogger(categoryName);//创建EFLogger类的实例
        }
        public void Dispose()
        {


        }

    }

    public class EFLoggerProvider : ILoggerProvider
    {
        public ILogger CreateLogger(string categoryName)
        {
            return new EFLogger(categoryName);
        }

        public void Dispose()
        {

        }
    }

    public class EFLogger : ILogger
    {
        protected string categoryName;

        public EFLogger(string categoryName)
        {
            this.categoryName = categoryName;
        }

        public IDisposable BeginScope<TState>(TState state)
        {

            return new NoopDisposable();

        }


        public bool IsEnabled(LogLevel logLevel)
        {
            if (logLevel == LogLevel.Information)
                return true;
            return false;
        }

        public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
        {

            if (categoryName == "Microsoft.EntityFrameworkCore.Database.Command" && logLevel == LogLevel.Information)
            {
                Debugger.Log(0, categoryName, "=============================== EF Core Sql日志 Start ===============================\r\n");
                Debugger.Log(0, categoryName, formatter(state, exception) + "\r\n");
                Debugger.Log(0, categoryName, "=============================== EF Core Sql日志 End===============================\r\n");
                Console.WriteLine("=============================== EF Core Sql日志 Start ===============================\r\n");
                Console.WriteLine(formatter(state, exception) + "\r\n");
                Console.WriteLine("=============================== EF Core Sql日志 End ===============================\r\n");
            }
        }
        private class NoopDisposable : IDisposable
        {
            public void Dispose()
            {
                int a = 1;
            }
        }
    }

}
namespace My.Core.Model
{
    public class MyDbContext : DbContext
    {
        // You can add custom code to this file. Changes will not be overwritten.
        // 
        // If you want Entity Framework to drop and regenerate your database
        // automatically whenever you change your model schema, please use data migrations.
        // For more information refer to the documentation:
        // http://msdn.microsoft.com/en-us/data/jj591621.aspx

        public MyDbContext(DbContextOptions options) : base(options)
        {

        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {

            base.OnModelCreating(modelBuilder);
        }

        //public static readonly LoggerFactory LoggerFactory_Debug = new LoggerFactory(new[] {
        //    new DebugLoggerProvider()
        //}, new LoggerFilterOptions() { MinLevel = LogLevel.Information });


        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (optionsBuilder.IsConfigured)
            {
            }
            base.OnConfiguring(optionsBuilder);
            //optionsBuilder.UseLoggerFactory(LoggerFactory_Debug);//第一种用chaj
            //optionsBuilder.UseLoggerFactory(new EFLoggerFactory());//第二种自定义工厂
            ILoggerProvider loggerProvider = new EFLoggerProvider();//三种ILoggerProvider
            ILoggerFactory loggerFactory = new LoggerFactory();
            loggerFactory.AddProvider(loggerProvider);
            optionsBuilder.UseLoggerFactory(loggerFactory);//第二种自定义工厂



        }
        

        public DbSet<FrameworkRole> FrameworkRoles { get; set; }

        public DbSet<FrameworkMenu> FrameworkMenus { get; set; }
        public DbSet<FrameworkRoleMenuPermission> FrameworkRoleMenuPermissions { get; set; }

        public DbSet<FrameworkUser> FrameworkUsers { get; set; }

        public DbSet<FrameworkUserRole> FrameworkUserRoles { get; set; }
    }
}

